<?php

namespace Frieza\Modules\Position\Actions;

use Doraemon\cache\EsUpdateCollector;
use Doraemon\cache\Queue;
use Doraemon\cache\SolrUpdateCollector;
use Doraemon\model\AdmOperateLog;
use Doraemon\service\search\AdmUserSearch;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Doraemon\model\AdmRecommendUser;
use Doraemon\model\AdmBestUser;
use Doraemon\model\Member;
use Doraemon\cache\User as CacheUser;

class SetUnAuthDesigner extends RowAction
{
    public $name = '变更为未认证设计师';
    const TOAST_ALERT = '确定将该用户的身份变更为未认证设计师吗?';

    public function handle($uid)
    {
        //将普通用户变更为未认证设计师
        $data['type'] = Member::AUTH_USER_UNAUTH_DESIGNER;
        $result = Member::updateUserInfo($uid,$data);
        //清除用户缓存
        CacheUser::deleteUserInfo($uid);
        //删除token,强制退出登录
        Queue::insertQueue('User/delTokenCache', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        //更新搜索的信息
        AdmUserSearch::updateAdmUserSearchByUid($uid);
        SolrUpdateCollector::insert($uid);
        EsUpdateCollector::insert($uid);
        Queue::insertQueue('User/updateSolr', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        if ($result){
            return $this->response()->success('已变更用户身份为未认证设计师')->refresh();
        }else{
            return $this->response()->error('出错啦,请稍后重试!!');
        }

    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }
}
